package com.avic.rong.fragment;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.avic.rong.MyApplication;
import com.avic.rong.R;
import com.avic.rong.activity.no.CriterionListActivity;
import com.example.minato.minastore.utils.AppUtils;
import com.github.lzyzsd.jsbridge.BridgeHandler;
import com.github.lzyzsd.jsbridge.BridgeWebView;
import com.github.lzyzsd.jsbridge.CallBackFunction;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;

import butterknife.BindView;
import butterknife.ButterKnife;

public class FragmentCategory extends Fragment {

    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";


    private String url;


    @BindView(R.id.webview)
    BridgeWebView webview;


    public FragmentCategory() {
    }

    public static FragmentCategory newInstance(String param1, String param2) {
        FragmentCategory fragment = new FragmentCategory();
        Bundle args = new Bundle();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView =  inflater.inflate(R.layout.fragment_fragment_category, container, false);
        ButterKnife.bind(this,rootView);


        if (MyApplication.getInstance().userLoginBean != null){
//            CookieManager cookieManager = CookieManager.getInstance();
//            cookieManager.setCookie(AppUtils.BASE_URL + "app/components/category?type=0", "userIdSign=" + MyApplication.getInstance().userLoginBean.getUserId()
//            +";domain=" + AppUtils.BASE_DOMAIN + ";path=/");
            url = AppUtils.BASE_URL + "app/components/category?type=0&userIdSign=" + MyApplication.getInstance().userLoginBean.getUserId();
        }else{
            url = AppUtils.BASE_URL + "app/components/category?type=0";
        }
        webview.loadUrl(url);



        webview.registerHandler("pushVc", new BridgeHandler() {
            @Override
            public void handler(String data, CallBackFunction function) {

                data = data.replace("\\","");
                JsonObject jsonObject = (JsonObject)new JsonParser().parse(data);
                String url = jsonObject.get("url").getAsString();

//                if (url.contains("?")){
//                    if (MyApplication.getInstance().userLoginBean != null){
//                        url = url + "&userIdSign=" + MyApplication.getInstance().userLoginBean.getUserId();
//                    }
//                }else{
//                    if (MyApplication.getInstance().userLoginBean != null){
//                        url = url + "?userIdSign=" + MyApplication.getInstance().userLoginBean.getUserId();
//                    }
//                }
                startActivity(CriterionListActivity.queryIntent(getActivity(),url));

//                function.onCallBack("submitFromWeb exe, response data from Java");
            }
        });
        return rootView;
    }

//    @Override
//    public void onStart() {
//        super.onStart();
//        EventBus.getDefault().register(this);
//    }
//
//    @Override
//    public void onStop() {
//        super.onStop();
//        EventBus.getDefault().unregister(this);
//    }
//
//    @Subscribe(threadMode = ThreadMode.MAIN,sticky = true)
//    public void onRefreshWebView(CategoryBean categoryBean){
//        if (webview != null){
////            webview.loadUrl(url);
//            webview.reload();
//        }
//    }



}
